<template>
  <div>
    <div style="padding-bottom: 10px; border-bottom: 4px solid #409eff">
      <el-card class="box-card">
        <div>
          <span style="margin-left: 33px; font-size: 10px; color: #909399">始发地:</span>
          <span style="margin-left: 246px; font-size: 10px; color: #909399">目的地:</span>
          <span style="margin-left: 280px; font-size: 10px; color: #909399">出发日期:</span>
        </div>
        <div>
          <el-select
            v-model="flightForm.cityFrom"
            filterable
            :required="true"
            :rules="[{ required: true, message: '请选择', trigger: 'change' }]"
            placeholder="始发地"
            @change="selFlightList"
            style="margin-left: 30px"
          >
            <el-option-group v-for="group in options" :key="group.label" :label="group.label">
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.value === flightForm.cityTo"
              ></el-option>
            </el-option-group>
          </el-select>
          <span style="margin-left: 28px">-</span>
          <el-select
            v-model="flightForm.cityTo"
            filterable
            :required="true"
            :rules="[{ required: true, message: '请选择', trigger: 'change' }]"
            placeholder="目的地"
            @change="selFlightList"
            style="margin-left: 30px"
          >
            <el-option-group v-for="group in options" :key="group.label" :label="group.label">
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.value === flightForm.cityFrom"
              ></el-option>
            </el-option-group>
          </el-select>
          <el-date-picker
            v-model="flightForm.departureData"
            type="date"
            placeholder="出发日期"
            style="margin-left: 100px"
            :picker-options="pickerOptions"
            @blur="selFlightList"
          ></el-date-picker>
        </div>
      </el-card>
    </div>
    <div style="padding: 10px 80px; background-color: #ebeef5; min-height: 500px;">
      <div>
        <el-table :data="flightList" style="width: 100%; margin: 0 auto">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="航空公司">
                  <span>{{ props.row.airline }}</span>
                </el-form-item>
                <el-form-item label="航班号">
                  <span>{{ props.row.flightNumber }}</span>
                </el-form-item>
                <el-form-item label="出发时间">
                  <span>{{ props.row.departureTime.replace("T", " ") }}</span>
                </el-form-item>
                <el-form-item label="到达时间">
                  <span>{{ props.row.arrivalTime.replace("T", " ") }}</span>
                </el-form-item>
                <el-form-item label="出发机场">
                  <span>{{ props.row.departureAirport }}</span>
                </el-form-item>
                <el-form-item label="到达机场">
                  <span>{{ props.row.arrivalAirport }}</span>
                </el-form-item>
                <el-form-item label="舱位">
                  <span>{{ props.row.selectedCabin }}</span>
                </el-form-item>
                <el-form-item label="价格">
                  <span>
                    {{
                    props.row.selectedCabin
                    ? props.row.cabins.find(
                    (cabin) => cabin.cabin === props.row.selectedCabin
                    ).price
                    : "-"
                    }}
                  </span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="航班信息" align="center">
            <template slot-scope="{ row }">
              <div>
                <img src="../assets/noImg.png" class="airline-icon" />
                <div style="color: #0086f6">{{ row.airline }} {{ row.flightNumber }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="出发信息" align="center">
            <template slot-scope="{ row }">
              <div class="time-info">
                <div class="time">{{ formatDate(row.departureTime) }}</div>
                <div class="airport">{{ row.departureAirport }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="60" align="center">
            <i class="el-icon-minus"></i>
          </el-table-column>
          <el-table-column label="到达信息" align="center">
            <template slot-scope="{ row }">
              <div class="time-info">
                <div class="time">{{ formatDate(row.arrivalTime) }}</div>
                <div class="airport">{{ row.arrivalAirport }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="舱位选择" align="center">
            <template slot-scope="{ row }">
              <el-select v-model="row.selectedCabin" placeholder="请选择舱位">
                <el-option
                  v-for="cabin in row.cabins"
                  :key="cabin.id"
                  :label="cabin.cabin"
                  :value="cabin.cabin"
                />
              </el-select>
              <div v-if="row.selectedCabin" class="cabin-display">当前选择：{{ row.selectedCabin }}</div>
            </template>
          </el-table-column>
          <el-table-column label="价格" align="center">
            <template slot-scope="{ row }">
              <div v-if="row.selectedCabin">
                <span style="color: #ff7d13; font-weight: bold; font-size: 16px">￥</span>
                <span style="color: #ff7d13; font-weight: bold; font-size: 22px">{{ price(row) }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" @click="booking(scope.row)">订票</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog
        :visible.sync="dialogVisible"
        title="填写订单信息"
        width="55%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <booking @my-event="cancelDig"></booking>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import Booking from "./Booking.vue";
import moment from "moment";
export default {
  components: { Booking },
  name: "MainContent",
  data() {
    return {
      dialogVisible: false, //订票弹框隐藏 显示
      flightList: [],
      flightForm: {
        cityFrom: "上海",
        cityTo: "北京",
        departureData: new Date(),
      },
      options: [
        {
          label: "热门城市",
          options: [
            {
              value: "上海",
              label: "上海",
              disabled: false,
            },
            {
              value: "北京",
              label: "北京",
              disabled: false,
            },
          ],
        },
        {
          label: "城市名",
          options: [
            {
              value: "成都",
              label: "成都",
              disabled: false,
            },
            {
              value: "深圳",
              label: "深圳",
              disabled: false,
            },
            {
              value: "广州",
              label: "广州",
              disabled: false,
            },
            {
              value: "大连",
              label: "大连",
              disabled: false,
            },
          ],
        },
      ],
      pickerOptions: {
        disabledDate(time) {
          const today = new Date();
          return time.getTime() < today.setHours(0, 0, 0, 0); //禁用今天以前的日期
        },
      },
    };
  },
  methods: {
    async selFlightList() {
      if (
        this.flightForm.cityFrom === "" ||
        this.flightForm.cityTo === "" ||
        this.flightForm.departureData === ""
      ) {
        this.$message.error("信息输入错误");
        return;
      }
      if (this.flightForm.cityFrom === this.flightForm.cityTo) {
        return;
      }
      const time = moment(this.flightForm.departureData);
      // 加上 8 小时
      const timeInEightHours = time.add(0, "hours");
      // 格式化时间字符串
      const formattedTime = timeInEightHours.format("YYYY-MM-DD HH:mm:ss");
      const t = {
        cityFrom: this.flightForm.cityFrom,
        cityTo: this.flightForm.cityTo,
        departureData: formattedTime,
      };
      // 在这里提交表单
      await this.$axios
        .post("/api/flight/sel", t)
        .then((res) => {
          if (res.data.code === 1) {
            const data = res.data.data.filter((contact) => {
              return contact.cabins.length > 0;
            });
            this.flightList = [...data];

            // this.$message.success(res.data.msg);
          } else if (res.data.code === 0) {
            this.flightList = [];
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          // 处理请求失败的错误
          if (error) {
            this.$message.error(error.response.data.msg);
          }
        });
    },
    // 打开订票弹框
    booking(row) {
      const flight = {};
      const newObj = Object.fromEntries(
        Object.entries(row).filter(([key, value]) => !Array.isArray(value))
      );
      const newObj1 = row.cabins.find(
        (cabin) => cabin.cabin === row.selectedCabin
      );
      Object.assign(flight, newObj, newObj1);

      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$bus.$emit("flight", flight);
      });
    },
    cancelDig(value) {
      this.dialogVisible = value;
    },
    // 根据舱位获取价格
    price(row) {
      if (!row) {
        return "";
      }
      if (!row.selectedCabin) {
        return "-";
      }
      const cabin = row.cabins.find((item) => item.cabin === row.selectedCabin);
      if (!cabin) {
        return "";
      }
      return cabin.price;
    },
    // 时间格式化
    formatDate(dateString) {
      const date = new Date(dateString);
      const time = date.toLocaleTimeString([], {
        hour: "2-digit",
        minute: "2-digit",
      });
      return time;
    },
  },
  created() {
    this.selFlightList();
  },
};
</script>

<style scoped>
.cabin-display {
  margin-top: 5px;
  font-size: 12px;
  color: #909399;
}
.airline-icon {
  width: 40px;
  height: 40px;
  margin-right: 12px;
}
/* 卡片样式 */
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 932.8px;
  margin-left: 20px;

  /* margin: 0 auto; */
}
/* 时间样式 */
.time-info {
  /* display: flex; */
  align-items: center;
}
.time {
  font-size: 20px;
  font-weight: bold;
}
.airport {
  font-size: 14px;
  color: #ccc;
  /* margin-left: 12px; */
}
.el-icon-arrow-right {
  margin: 0 12px;
}

/* 航班详细信息的样式 */
.demo-table-expand {
  font-size: 0;
  text-align: center;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>